using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using jQueryApi;

namespace Nsb.Classes
{
    [IgnoreNamespace]
    public static class DropTarget
    {
        public static void DragOverEv(jQueryEvent e)
        {
            DomElement.CancelEvent(e);
        }

        //public static void DropDumpEv(jQueryEvent e)
        //{
        //    Dictionary result = DropObject(e);
        //    //Script.Literal("debugger");
        //    Inform.Debug("MouseX {0}, {1}", result["mouseX"], e.OffsetX);
        //    Inform.Dump(result);
        //}


        /// <summary>
        /// Creates a dicitonary of the drop attributes.
        /// Most useful are "url" and "html".
        /// </summary>
        /// <param name="e">The event</param>
        /// <returns>Dictionary</returns>
        public static Dictionary DropObject(jQueryEvent e)
        {
            DomElement.CancelEvent(e);

            Dictionary result = new Dictionary();

            jQueryObject target = jQuery.FromElement(e.Target);

            result["mouseX"] = Type.GetField(e, "pageX") ?? Script.Literal("{0}.originalEvent.pageX", e);
            result["mouseY"] = Type.GetField(e, "pageY") ?? Script.Literal("{0}.originalEvent.pageY", e);
            result["target"] = target;
            result["selectTo"] = target.Plugin<Jewl>().SelecTo();

            try
            {
                List<string> types = (List<string>) Script.Literal("{0}.originalEvent.dataTransfer.types", e);
                result["types"] = types;

                //foreach (string type in types)
                //{
                //    Inform.Debug("type {0}", type);
                //}

                if (types.Contains("url"))
                {
                    string data = (string) Script.Literal("{0}.originalEvent.dataTransfer.getData({1})", e, "url");
                    if (data != null)
                        result["imgurl"] = GetUrl(data);
                }

                if (types.Contains("text/html"))
                {
                    string data = Script.Literal("{0}.originalEvent.dataTransfer.getData({1})", e, "text/html").ToLocaleString();
                    result["html"] = data;
                    jQueryObject el = jQuery.FromHtml(data);
                    string src = el.Find("img").GetAttribute("src");
                    if (String.IsNullOrEmpty(src))
                        src = el.GetAttribute("src");
                    result["srcurl"] = src;
                    int idx = data.IndexOf("imgurl=");
                    if (idx > 0)
                    {
                        string u = data.Substr(idx + 7).Split("&")[0];
                        result["htmlurl"] = u.DecodeUriComponent();
                    }
                }

                if (types.Contains("text/uri-list"))
                {
                    string data = (string) Script.Literal("{0}.originalEvent.dataTransfer.getData({1})", e, "text/uri-list");
                    //Inform.Debug("data {0}", data);
                    result["uriurl"] = GetUrl(data);
                }

                if (types.Contains("text"))
                {
                    string data = (string) Script.Literal("{0}.originalEvent.dataTransfer.getData({1})", e, "text");
                    result["texturl"] = GetUrl(data);
                }

                if (types.Contains("text/plain"))
                {
                    string data = (string) Script.Literal("{0}.originalEvent.dataTransfer.getData({1})", e, "text/plain");
                    result["texturl2"] = GetUrl(data);
                }
            }
            catch //(Exception ex)
            {
                //Inform.Debug("DropTarget excetion: " + ex);
            }
            result["url"] = result["imgurl"] ?? result["htmlurl"] ?? result["texturl"] ?? result["texturl2"] ?? result["uriurl"] ?? result["srcurl"];
            if (!Script.IsValue(result["url"]))
                result["url"] = null;
            else if (!(bool) Script.Literal(@"/^https?:/.test({0})", result["url"]))
                result["url"] = "http://" + result["url"];

            return result;
        }

        private static string GetUrl(string txt)
        {
            string q = txt.Split("?")[1] ?? txt;
            Dictionary p = Uri.Pairs(q);
            return (string) p["imgurl"] ?? txt;
        }
    }
}